#-*- coding:utf-8 -*-
import cv2
import sys
import threading
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class Display:
    def __init__(self, ui, mainWnd):
        self.ui = ui
        self.mainWnd = mainWnd

        # 默认视频源为相机
        self.ui.radioButtonCam.setChecked(True)
        self.isCamera = True

        # 信号槽设置
        ui.Open.clicked.connect(self.Open)
        ui.Close.clicked.connect(self.Close)
        ui.radioButtonCam.clicked.connect(self.radioButtonCam)
        ui.radioButtonFile.clicked.connect(self.radioButtonFile)

        # 创建一个关闭事件并设为未触发
        self.stopEvent = threading.Event()
        self.stopEvent.clear()

    def radioButtonCam(self):
        self.isCamera = True

    def radioButtonFile(self):
        self.isCamera = False

    def Open(self):
        if not self.isCamera:
            self.fileName, self.fileType = QFileDialog.getOpenFileName(self.mainWnd, 'Choose file', '', '*.mp4')
            self.cap = cv2.VideoCapture(self.fileName)
            self.frameRate = self.cap.get(cv2.CAP_PROP_FPS)
        else:
            # 下面两种rtsp格式都是支持的
            # cap = cv2.VideoCapture("rtsp://admin:Supcon1304@172.20.1.126/main/Channels/1")
            self.cap = cv2.VideoCapture("rtsp://admin:Supcon1304@172.20.1.126:554/h264/ch1/main/av_stream")

        # 创建视频显示线程
        th = threading.Thread(target=self.Display)
        th.start()

    def Close(self):
        # 关闭事件设为触发，关闭视频播放
        self.stopEvent.set()

    def Display(self):
        self.ui.Open.setEnabled(False)
        self.ui.Close.setEnabled(True)

        while self.cap.isOpened():
            success, frame = self.cap.read()
            # RGB转BGR
            frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
            img = QImage(frame.data, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
            self.ui.DispalyLabel.setPixmap(QPixmap.fromImage(img))

            if self.isCamera:
                cv2.waitKey(1)
            else:
                cv2.waitKey(int(1000 / self.frameRate))

            # 判断关闭事件是否已触发
            if True == self.stopEvent.is_set():
                # 关闭事件置为未触发，清空显示label
                self.stopEvent.clear()
                # self.ui.DispalyLabel.clear()
                # self.ui.Close.setEnabled(False)
                # self.ui.Open.setEnabled(True)
                break






class CGAIRTMP(QWidget):

    def __init__(self):
        super(CGAIRTMP, self).__init__()

        self.main_layout = QHBoxLayout()
        self.config_layout = QVBoxLayout()
        self.rtmp_layout = QHBoxLayout()
        self.rtmp_label = QLabel('rtmp:')
        self.rtmp_edit = QLineEdit()
        self.rtmp_layout.addWidget(self.rtmp_label)
        self.rtmp_layout.addWidget(self.rtmp_edit)
        self.btn_layout = QHBoxLayout()
        self.start_btn = QPushButton('播放')
        self.start_btn.clicked.connect(self._start)
        self.end_btn = QPushButton('关闭')
        self.btn_layout.addWidget(self.start_btn)
        self.btn_layout.addWidget(self.end_btn)
        self.config_layout.addLayout(self.rtmp_layout)
        self.config_layout.addLayout(self.btn_layout)
        self.video_label = QLabel()
        self.main_layout.addLayout(self.config_layout)
        self.main_layout.addWidget(self.video_label)
        self.setLayout(self.main_layout)


    def _start(self):
        rtmp_path = self.


if __name__ == '__main__':
    app = QApplication(sys.argv)
    c = CGAIRTMP()
    c.show()
    sys.exit(app.exec_())

